//
//  LikeTableViewController.m
//  DeliciousFood1
//
//  Created by brother on 15/10/23.
//  Copyright © 2015年 com.hxm. All rights reserved.
//

#import "LikeTableViewController.h"
#import <AFNetworking.h>
#import "AppDelegate.h"
#import "User.h"
#import "ShowTableViewController.h"

@interface LikeTableViewController ()
@property (nonatomic,strong) NSMutableArray *array;

- (IBAction)back:(id)sender;
@end

@implementation LikeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        NSString *url = @"http://localhost:8888/thinkphp/index.php/home/food/findLikeFood";
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        AppDelegate *application =  [[UIApplication sharedApplication] delegate] ;
        
        dic[@"userid"] = [NSNumber numberWithInt:(application.user.ID)];
        
        [manager GET:url parameters:dic success:^(AFHTTPRequestOperation * operation, id  responseObject) {
            NSString *url1 = @"http://localhost:8888/thinkphp/index.php/home/food/findFoodID";
            NSArray *array1 = (NSArray *)responseObject;
            
            for (int i=0; i< array1.count; i++) {
                NSDictionary *dic2 = array1[i];
                NSMutableDictionary *dic3 = [NSMutableDictionary dictionary];
                dic3[@"id"] = dic2[@"foodid"];
                [manager GET:url1 parameters:dic3 success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.array addObject:(NSArray *)responseObject];
                        [self.tableView reloadData];
                    });
                } failure:^(AFHTTPRequestOperation *  operation, NSError *  error) {
                    
                }];
            }
            
            NSLog(@"%ld",self.array.count);
        } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
            
        }];
    });
}

//懒加载
- (NSMutableArray *)array
{
    if (_array == nil) {
        _array = [NSMutableArray array];
    }
    return _array;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    
    NSArray *arr = self.array[indexPath.row];
    NSDictionary *dic = arr[0];
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:dic[@"picture"]]];
    cell.imageView.image = [UIImage imageWithData:data];
    
    cell.textLabel.text = dic[@"name"];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = self.array[indexPath.row];
    NSDictionary *dic = arr[0];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSString *url1 = @"http://localhost:8888/thinkphp/index.php/home/food/findFoodStep";
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"foodid"] = dic[@"id"];
    [manager GET:url1 parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *array = (NSArray *)responseObject;
        NSMutableArray *mu = [NSMutableArray array];
        for (NSDictionary *dictionary in array) {
            NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
            [dic1 setObject:dictionary[@"picture"] forKey:@"img"];
            [dic1 setObject:dictionary[@"description"] forKey:@"step"];
            [mu addObject:dic1];
        }
        dict[@"albums"] = dic[@"picture"];
        dict[@"title"] = dic[@"name"];
        dict[@"intro"] = dic[@"description"];
        dict[@"steps"] = mu;
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ShowTableViewController *show = [storyBoard instantiateViewControllerWithIdentifier:@"show"];
        show.dic = dict;
        [self presentViewController:show animated:YES completion:nil];
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        
    }];
    
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
